/**
 * Crazy Table Verify
 *  License by CrazyYi.
 *  v1.0.0
 *
 *
 */
layui.define(['table', 'jquery', 'form', 'layer', 'laydate', 'crazyTool'], function (exports) {
    const MOD_NAME = 'crazyVerify'

    const STR_OR = '|'

    const $ = layui.$,
        form = layui.form,
        crazyTool = layui.crazyTool

    /**
     * 表单验证功能增强，如果校验通过，千万不能有返回值
     */
    form.verify({
        "radio-required"(value, item) {
            //单选按钮必选
            let va = $(item).find("input[type='radio']:checked").val();
            if (typeof (va) == "undefined") {
                return $(item).attr("lay-verify-msg");
            }
        },
        "checkbox-required"(value, item) {
            //复选框必选
            let va = $(item).find("input[type='checkbox']:checked").val();
            if (typeof (va) == "undefined") {
                return $(item).attr("lay-verify-msg");
            }
        },
        "required"(value, item) {
            if (value === undefined && value == null) {
                return '必填项不能为空'
            }
            if (value.trim() === '') {
                return '必填项不能为空'
            }
        },
        "abc"(value, item) {
            if (!(/^\w+$/.test(value))) {
                return '只能填写字母、数字及下划线'
            }
        },
        "email"(value, item) {
            if (crazyTool.isBlank(value)) {
                return
            }
            if (!/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(value)) {
                return '邮箱格式不正确'
            }
        },
        "number"(value, item) {
            if (crazyTool.isNotBlank(value) && isNaN(value)) {
                return '只能填写数字'
            }
        },
        "phone"(value, item) {
            if (crazyTool.isBlank(value)) {
                return
            }
            if (!/^1\d{10}$/.test(value)) {
                return '请输入正确的手机号'
            }
        },

        "integer"(value, item) {
            if (crazyTool.isBlank(value)) {
                return
            }
            if (!crazyTool.isIntNum(value)) {
                return '只能填写整数'
            }
        },

        "positive_integer": [/^\d+$/, "只能填写正整数"],
        "negative_integer": [/^-\d+$/, "只能填写负整数"]

        // integer   /^-?\d+$/
        // digits  /^(0|\+?[1-9][0-9]*)$/
        //     /^(([^0][0-9]+|0)\.([0-9]{1,2}))$/
    });

    /**
     * 规则验证，返回校验失败的字符串
     * @type {{verify: verify, titles: {date: string, number: string, phone: string, identity: string, required: string, email: string, url: string}, placeholder: placeholder, class: class}}
     */
    const layVerify = {
        titles: {
            required: '必填',
            phone: '手机',
            email: '邮箱',
            url: '网址',
            number: '数字',
            date: '日期',
            identity: '身份证',
            integer: '整数'
        },
        placeholder: function (verify, defaultStr) {
            if (defaultStr) {
                return defaultStr;
            }
            let verifies = verify ? verify.split(STR_OR) : [];
            let str = '';
            for (let k in verifies) {
                if (verifies.hasOwnProperty(k)) {
                    if (this.titles.hasOwnProperty(verifies[k])) {
                        str += STR_OR + this.titles[verifies[k]];
                    }
                }
            }
            str = str.replace(STR_OR, '');
            return str;
        },
        class: function (rowIndex) {
            // log.print('set class rowIndex', rowIndex);
            if (rowIndex % 2) {
                return 'crazy-layui-input-inline crazy-layui-input crazy-layui-input-even';
            } else {
                return 'crazy-layui-input-inline crazy-layui-input';
            }
        },
        verify: function (verify, value, cell) {
            let v = layui.form.config.verify[verify];
            if (!v) {
                return true;
            }

            // return undefined or false，代表校验成功, 具体格式详见  layui.form.config.verify
            let r, msg, res;
            if (typeof v === 'function') {
                r = v(value, cell);
                res = r === undefined;
                msg = res ? '' : r;
            } else if (v.constructor === Array) {
                r = v[0].test(value);
                res = r == true;
                msg = res ? '' : v[1];
            } else {
                return true;
            }
            return {res: res, msg: msg};
        },
    };

    //输出接口
    exports(MOD_NAME, layVerify);
});